package com.ews.onlineexchange.controller.activitii;

import java.util.List;

import javax.enterprise.inject.Model;
import javax.inject.Inject;

import com.ews.onlineexchange.model.User;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.apache.commons.lang3.StringUtils;
import com.ews.onlineexchange.service.EntityQuery;
import com.ews.onlineexchange.service.UserService;

@Model
public  class HistoricBpmList {
	
	@Inject
	protected HistoryService		historyService;

	@Inject
	protected UserService			userService;

	protected String				processKey;
	
	protected Integer				firstResult	= 0;
	protected Integer				maxResults	= EntityQuery.MAX_RESULTS_FEW;
	private   Boolean				flag ;   //审核标志
	private   String				supplierName;
	private   String				name;
	private   String				dictId;
	private	  String				assignee;
	private   String				projectName;
	Long							resultCount;
	List<HistoricTaskInstance>						resultList;
	
	public List<HistoricTaskInstance> getResultList() {
		if ( this.resultList == null ) {
			initResultList();
		}
		return this.resultList;
	}

	private void initResultList() {
		HistoricTaskInstanceQuery historicTaskInstanceQuery =  historyService.createHistoricTaskInstanceQuery();
		historicTaskInstanceQuery.includeTaskLocalVariables();
		historicTaskInstanceQuery.includeProcessVariables();
	
		if ( StringUtils.isNotBlank( getProcessKey() ) ) {
			historicTaskInstanceQuery.processDefinitionKey( getProcessKey() );
		}

		String username = userService.getLoginedUser().getUsername();
		if(StringUtils.isNoneBlank(supplierName)) {
			historicTaskInstanceQuery.processVariableValueLike("supplierName", getStringRestriction(supplierName));
		}
		
		if(StringUtils.isNoneBlank(name)) {
			historicTaskInstanceQuery.taskNameLike(getStringRestriction(name));
		}
		if(StringUtils.isNoneBlank(dictId)) {
			historicTaskInstanceQuery.processVariableValueEquals("dictId", dictId);
		}
		if(StringUtils.isNoneBlank(projectName)) {
			historicTaskInstanceQuery.processVariableValueEquals("projectName", projectName);
		}
		if(StringUtils.isNoneBlank(assignee)) {
			historicTaskInstanceQuery.taskAssigneeLike(getStringRestriction(assignee));
		}
		historicTaskInstanceQuery.orderByHistoricTaskInstanceEndTime().desc();
		if(!userService.hasRole("developer")) {
			historicTaskInstanceQuery.taskAssignee(username);
		}
		User user = userService.getLoginedUser();
		if(user != null){
			String userRegion = user.getRegion();
			//针对择优选取和取消报名，为了筛选方便，supplierRegion表示的是buyerRegion
			if("370501".equals(user.getRegion()))historicTaskInstanceQuery.processVariableValueLessThanOrEqual("supplierRegion",user.getRegion());
			else historicTaskInstanceQuery.processVariableValueEquals("supplierRegion",user.getRegion());
		}
		historicTaskInstanceQuery.finished();
		List<HistoricTaskInstance> list = null;
		if ( getMaxResults() == null ) {
			list = historicTaskInstanceQuery.list();
		} else {
			list = historicTaskInstanceQuery.listPage( getFirstResult(), getMaxResults() );			
		}
		this.resultCount = historicTaskInstanceQuery.count();		
		
		this.resultList = list;
	}

	public String getProcessKey() {
		return processKey;
	}

	public void setProcessKey( String processKey ) {
		this.processKey = processKey;
	}

	public Integer getFirstResult() {
		return firstResult;
	}

	public void setFirstResult( Integer firstResult ) {
		this.firstResult = firstResult;
	}

	public Integer getMaxResults() {
		return maxResults;
	}

	public void setMaxResults( Integer maxResults ) {
		this.maxResults = maxResults;
	}
	
	public Long getResultCount() {
		return resultCount;
	}

	public void setResultCount( Long resultCount ) {
		this.resultCount = resultCount;
	}

	public boolean isNextExists() {
		getResultList();
		return resultList!=null && getMaxResults()!=null &&
	             resultList.size() > getMaxResults();
	}
	
	public boolean isPreviousExists() {
		return getFirstResult() != null && getFirstResult() != 0;
	}
	
	public boolean isPaginated() {
		return isNextExists() || isPreviousExists();
	}

	public Long getLastFirstResult() {
		Integer pc = getPageCount();
		return pc == null ? null : (pc.longValue() - 1) * getMaxResults();
	}

	/**
	 * Get the index of the first result of the next page
	 * 
	 */
	public int getNextFirstResult() {
		Integer fr = getFirstResult();
		return (fr == null ? 0 : fr) + getMaxResults();
	}

	/**
	 * Get the index of the first result of the previous page
	 * 
	 */
	public int getPreviousFirstResult() {
		Integer fr = getFirstResult();
		Integer mr = getMaxResults();
		return mr >= (fr == null ? 0 : fr) ? 0 : fr - mr;
	}
   
	/**
	 * Get the total number of pages
	 * 
	 */
	public Integer getPageCount() {
		if (getMaxResults() == null) {
			return null;
		} else {
			int rc = getResultCount().intValue();
			int mr = getMaxResults().intValue();
			int pages = rc / mr;
			return rc % mr == 0 ? pages : pages + 1;
		}
	}

	public Boolean isFlag() {
		return flag;
	}

	public void setFlag(Boolean flag) {
		this.flag = flag;
	}

	public String getAssignee() {
		return assignee;
	}

	public void setAssignee(String assignee) {
		this.assignee = assignee;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSupplierName() {
		return supplierName;
	}

	public void setSupplierName(String supplierName) {
		this.supplierName = supplierName;
	}
	
	public String getDictId() {
		return dictId;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public void setDictId(String dictId) {
		this.dictId = dictId;
	}

	public static String getStringRestriction(String value) {
		if (value == null)
			value = "";
		return "%" + value + "%";
	}
	
}
